<template>
	<view class="wrapper">
		<form @submit="userEdit">
		<view class="content">
			<view class="inp-item" v-if="type == '1'">
				<view class="inp-text">地址</view>
				<view class="input">
					<input placeholder="地址" name="address" :value="UserInfo.address" placeholder-class="plass" />
				</view>
			</view>
			
			<view class="inp-item" v-if="type == '2'">
				<view class="inp-text">用户名</view>
				<view class="input">
					<input placeholder="用户名" name="company" :value="UserInfo.company" placeholder-class="plass" />
				</view>
			</view>
			
			<button class="confirm-btn" formType="submit" >修改</button>
		</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				UserInfo:[],
				type:'2'
				
			}
		},
		onLoad(e) {
			if(e.type){
				this.type = e.type;
			}
			
			if(uni.getStorageSync('store_id') == ''){
				this.$api.errorMsg('你还没有登陆哦~');
				setTimeout(function () {
					uni.redirectTo({
						url:'./login'
					})
				}, 2000);
				
			}else{
				this.UserInfo = JSON.parse(uni.getStorageSync('storeMsg'));
			}
			
		},
		methods: {
			//修改个人信息
			userEdit:function(e){
				var that = this;
				var data = e.detail.value;
				var store_id = uni.getStorageSync('store_id');
				var text = {};
				
				text.store_id = store_id;
				if(this.type == '2'){
					text.company = data.company
				}else{
					text.address = data.address
				}
				
				uni.request({
					url: that.$websiteUrl + '/index.php/Store/User/userEdit.html', 
					data: text,
					method:'POST',
					dataType:'json',
					header: {'content-type' : 'application/x-www-form-urlencoded'},
					success: (resa) => {
						that.$api.HideWaitModel();
						var one = resa.data;
						if(one.status == '1'){
							var UserInfo = JSON.parse(uni.getStorageSync('storeMsg'));
							if(this.type == '2'){
								UserInfo.company = data.company;
							}else{
								UserInfo.address = data.address;
							}
							
							uni.setStorageSync('storeMsg',JSON.stringify(UserInfo))
							
							that.$api.successMsg(one.message);
							setTimeout(function () {
								uni.navigateBack({
								    delta: 1
								});
							}, 2000);
						
						}else{
							that.$api.errorMsg(one.message);
						}
					},
					fail: (resa) => {
						that.$api.HideWaitModel();
						that.$api.errorMsg('网络连接超时，请检查您的网络');
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 45upx 30upx 30upx;
	}
	
	.inp-item {
		padding: 15upx 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		line-height: 75upx;
		.inp-text {
			font-size: 28upx;
			color: #333;
		}
		.input {
			width: 490upx;
			height: 75upx;
			border-bottom: 1upx solid #eee;
			input {
				width: 100%;
				height: 100%;
				font-size: 28upx;
				color: #333;
			}
			.plass {
				font-size: 28upx;
				color: #ccc;
			}
		}
	}
	
	.confirm-btn {
		margin: 125upx auto 0;
		width: 560upx;
		height: 70upx;
		background: #f70000;
		border-radius: 40upx;
		font-size: 28upx;
		text-align: center;
		line-height: 70upx;
		letter-spacing: 1px;
		color: #fff;
	}
	
</style>
